//
//  BPSettingViewController.m
//  BabyPhotograph
//
//  Created by cesh on 2017/2/6.
//  Copyright © 2017年 cesh. All rights reserved.
//

#import "BPSettingViewController.h"
#import <SDImageCache.h>
@interface BPSettingViewController ()

@property (weak, nonatomic) IBOutlet UIButton *logout;
@property (weak, nonatomic) IBOutlet UIView *clearCacheView;
@property (weak, nonatomic) IBOutlet UILabel *cacheLbl;

@end

@implementation BPSettingViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 1.0;
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 1.0;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"系统设置";
    self.logout.layer.cornerRadius = 8.0;
    
    //获取缓存空间的大小
    NSInteger size = [[SDImageCache sharedImageCache]getSize];
    float sizeM = size * 1.0 / 1024 / 1024;
    self.cacheLbl.text = [NSString stringWithFormat:@"%.1lf MB", sizeM];
    
    [self.clearCacheView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearCache)]];
}

- (void)clearCache {
    [SVProgressHUD showWithStatus:@"删除中..."];
    [[SDImageCache sharedImageCache]clearDiskOnCompletion:^{
        [SVProgressHUD dismiss];
        NSInteger size = [[SDImageCache sharedImageCache]getSize];
        float sizeM = size * 1.0 / 1024 / 1024;
        self.cacheLbl.text = [NSString stringWithFormat:@"%.1lf MB", sizeM];
        
    }];
}

- (IBAction)logOutBtn:(id)sender {
    //判定用户是否已登陆
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"login"]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"退出账号" message:@"确定要退出账号吗？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            //退出账号
            [SVProgressHUD showWithStatus:@"退出中..."];
            NSDictionary *user = [[NSUserDefaults standardUserDefaults]objectForKey:@"user"];
            AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
            mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                             @"text/html",
                                                             @"image/jpeg",
                                                             @"image/png",
                                                             @"application/octet-stream",
                                                             @"text/json",
                                                             nil];
            
            [mgr POST:[NSString stringWithFormat:@"%@?c=User&&a=logout",requestURL]
           parameters:@{@"token" : user[@"token"]}
             progress:^(NSProgress * _Nonnull uploadProgress) {
                 
             } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                 if ([responseObject[@"state"] isEqualToString:@"SUCCESS"]) {
                     [SVProgressHUD showInfoWithStatus:@"退出成功"];
                     //设置为未登录
                     [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"login"];
                     //删除原来的用户数据
                     [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"user"];
                     [[NSNotificationCenter defaultCenter]postNotificationName:@"changeUserHeader" object:nil];
                     [[NSNotificationCenter defaultCenter]postNotificationName:@"changeUserNickname" object:nil];
                     [SVProgressHUD dismiss];
                 }
                 else {
                     [SVProgressHUD showInfoWithStatus:@"请求错误"];
                     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                         [SVProgressHUD dismiss];
                     });
                 }
             } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                 [SVProgressHUD showInfoWithStatus:@"网络故障"];
                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                     [SVProgressHUD dismiss];
                 });
             }];
        }];
         UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:confirm];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:nil];
    }
    else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您还没有登录" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:confirm];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
}

@end
